package com.maning.mndialoglibrary;


import com.maning.mndialoglibrary.base.BaseDialog;
import com.maning.mndialoglibrary.config.MDialogConfig;
import com.maning.mndialoglibrary.utils.TextUtils;
import com.maning.mndialoglibrary.view.MNHudCircularProgressBar;
import com.maning.mndialoglibrary.view.MNHudProgressWheel;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * Created by maning on 2017/8/9.
 * LoadingDialog
 */

public class MProgressDialog {

    private static BaseDialog mDialog;
    private static MDialogConfig mDialogConfig;

    //布局
    private static DependentLayout dialog_window_background;
    private static DependentLayout dialog_view_bg;
    private static MNHudProgressWheel progress_wheel;
    private static Text tv_show;


    private static void initDialog(Context mContext) {
        checkDialogConfig();
        try {
            LayoutScatter inflater = LayoutScatter.getInstance(mContext);

            Component mProgressDialogView = inflater.parse(ResourceTable.Layout_mn_progress_dialog_layout, null,false);
            mDialog = new BaseDialog(mContext);
            mDialog.setTransparent(true);
            //获取布局
            dialog_window_background = (DependentLayout) mProgressDialogView.findComponentById(ResourceTable.Id_dialog_window_background);
            dialog_view_bg = (DependentLayout) mProgressDialogView.findComponentById(ResourceTable.Id_dialog_view_bg);
            tv_show = (Text) mProgressDialogView.findComponentById(ResourceTable.Id_tv_show);
            progress_wheel = (MNHudProgressWheel) mProgressDialogView.findComponentById(ResourceTable.Id_progress_wheel);
            progress_wheel.spin();
            mDialog.setContentCustomComponent(mProgressDialogView);
            //配置相关
            configView(mContext);
        } catch (Exception e) {
            e.printStackTrace();
            //Log.e(">>>MProgressDialog>>>", "MProgressDialog-initDialog异常:" + e.toString());
        }
    }

    private static void checkDialogConfig() {
        if (mDialogConfig == null) {
            mDialogConfig = new MDialogConfig.Builder().build();
        }
    }

    private static void configView(Context mContext) {

        //Progress设置
        progress_wheel.setBarColor(mDialogConfig.progressColor);
        progress_wheel.setBarWidth((int)mDialogConfig.progressWidth);
        progress_wheel.setRimColor(mDialogConfig.progressRimColor);
        if(mDialogConfig.progressRimWidth!=0){
            progress_wheel.setRimWidth(mDialogConfig.progressRimWidth);
        }

        ComponentContainer.LayoutConfig layoutParamsProgress = progress_wheel.getLayoutConfig();
        layoutParamsProgress.width =  (int)mDialogConfig.progressSize;
        layoutParamsProgress.height = (int)mDialogConfig.progressSize;
        progress_wheel.setLayoutConfig(layoutParamsProgress);
        //文字颜色设置
        tv_show.setTextColor(mDialogConfig.textColor);
        tv_show.setTextSize((int)mDialogConfig.textSize);
        //点击事件
        dialog_window_background.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mDialogConfig != null && mDialogConfig.canceledOnTouchOutside) {
                    dismissProgress();
                }
            }
        });
    }

    public static void showProgress(Context context) {
        showProgress(context, null,null);
    }

    public static void showProgress(Context context, CharSequence msg) {
        showProgress(context, msg, null);
    }

    public static void showProgress(Context context, MDialogConfig mDialogConfig) {
        showProgress(context, null, mDialogConfig);
    }

    public static void showProgress(Context context, CharSequence msg, MDialogConfig dialogConfig,int duration) {
        dismissProgress();
        try {
            //设置配置
            if (dialogConfig == null) {
                dialogConfig = new MDialogConfig.Builder().build();
            }
            mDialogConfig = dialogConfig;



            initDialog(context);
            if (mDialog != null && tv_show != null) {
                if (TextUtils.isEmpty(msg.toString())) {
                    tv_show.setVisibility(Component.HIDE);
                } else {
                    tv_show.setVisibility(Component.VISIBLE);
                    tv_show.setText(msg.toString());
                }
                if(duration>0){
                    mDialog.setAutoClosable(true);
                    mDialog.setDuration(duration);
                }
                mDialog.show();
            }
        } catch (Exception e) {
            //Log.e(">>>MProgressDialog>>>", "MProgressDialog-showProgress异常:" + e.toString());
        }
    }

    public static void showProgress(Context context, CharSequence msg, MDialogConfig dialogConfig) {
        dismissProgress();
        try {
            //设置配置
            if (dialogConfig == null) {
                dialogConfig = new MDialogConfig.Builder().build();
            }
            mDialogConfig = dialogConfig;

            initDialog(context);
            if (mDialog != null && tv_show != null) {
                if (TextUtils.isEmpty(msg.toString())) {
                    tv_show.setVisibility(Component.HIDE);
                } else {
                    tv_show.setVisibility(Component.VISIBLE);
                    tv_show.setText(msg.toString());
                }
                mDialog.show();
            }
        } catch (Exception e) {
            //Log.e(">>>MProgressDialog>>>", "MProgressDialog-showProgress异常:" + e.toString());
        }
    }

    public static void dismissProgress() {
        try {
            if (mDialog != null && mDialog.isShowing()) {
                //判断是不是有监听
                if (mDialogConfig != null && mDialogConfig.onDialogDismissListener != null) {
                    mDialogConfig.onDialogDismissListener.onDismiss();
                    mDialogConfig.onDialogDismissListener = null;
                }
                mDialog.hide();
            }
        } catch (Exception e) {
            //Log.e(">>>MProgressDialog>>>", "MProgressDialog-dismissProgress异常:" + e.toString());
        } finally {
            releaseDialog();
        }
    }

    private static void releaseDialog() {
        mDialogConfig = null;
        mDialog = null;
        dialog_window_background = null;
        dialog_view_bg = null;
        progress_wheel = null;
        tv_show = null;
    }

    public static boolean isShowing() {
        if (mDialog != null) {
            return mDialog.isShowing();
        }
        return false;
    }
}
